def read_csv(filename):
  """读取csv文件"""
  with open(filename, mode="r", encoding="utf-8") as f:
    headerline = f.readline().strip()
    titles = headerline.split(',')
    datalines = f.readlines()
    result = []
    for dataline in datalines:
      datas = dataline.strip().split(',')
      row = {}
      for i in range(len(titles)):
        row[titles[i]] = datas[i]
      result.append(row)  
    return result
    

def list_to_markdowntable(list, cols=4):
  """转为markdown table"""
  result = ''
  while len(list) > 0:
    result = result + list_to_markdowntable_row(list, cols) + '\n'
    list = list[cols:]
  return result  

def list_to_markdowntable_row(list, cols):
  """转为markdown table行"""
  row = '| '
  for i in range(cols):
    cell = list[i] if i < len(list) else ''
    row = row + cell + ' | '
  return row

def save_markdowntable(mktable, filename, section):
  """保存"""
  pass


if __name__ == "__main__":
  csvRows = read_csv(r"D:\LiuZhaoHui\Note\Note\CSharp\关键字列表.csv")
  csvRows = [r for r in csvRows if r["上下文关键字"] == 'Y']
  keys = list(map(lambda x : x["关键字"], csvRows))
  mktable = list_to_markdowntable(keys, 6)
  print(mktable)
  # save_markdowntable(mktable, "", "")

  

